/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package jpa.converters;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import org.jdesktop.beansbinding.Converter;

/**
 *
 * @author innovapps.co
 */

public class DateConverter extends Converter {

        public DateConverter(){
        }

        @Override
        public Object convertForward(Object arg){

            // Convert Date object into String
            try{
                if( arg != null && arg.getClass()==java.util.Date.class){
                    String date = new SimpleDateFormat("yyyy/MM/dd").format(arg);
                    return date;
                }
            }catch(Exception ex){
                System.out.println("dateConverter(): " + ex.getMessage());
            }
            return arg;
        }

        @Override
        public Object convertReverse(Object arg){  
            // JLabel is "read-only"

            // Add your reverse code when it is a combo box, text field etc.
            // that returns Date object, for example:

            java.util.Date date = new java.util.Date();  // Default for error handling
            try{
                DateFormat formatter = new SimpleDateFormat("yyyy/MM/dd");
                date = formatter.parse( (String)arg );   // String to Date object
            }catch(Exception ex){
                System.out.println("dateConverter.convertReverse error: " + ex.getMessage());
            }
            return date;   // return Date object
        }
    }
